package mini.expr.lists;
import java.util.List;
import java.util.LinkedList;

import mini.Env;
import mini.expr.Element;
import mini.expr.Expr;


public class Cons extends Expr {
    private Expr e1;
    private Expr e2;
    
    public Cons(Expr e1, Expr e2) {
        this.e1 = e1;
        this.e2 = e2;
    }

    public Element eval(List<Env> listOfEnvs, LinkedList var) {
        Element car = e1.eval(listOfEnvs, var);
        Element cdr = e2.eval(listOfEnvs, var);
        
        if (!cdr.isList()) {
            throw new IllegalStateException("Expression does not evaluate to a list.");
        }
        
        Element cdrClone = cdr.clone();
        cdrClone.getListValue().getSequence().addToFront(car.clone());
        
        return cdrClone;
    }
}
